/*
 * Created on 2005-3-12
 */
package com.hd123.rumba.domain;

import java.util.ArrayList;
import java.util.List;

import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;

import org.apache.log4j.Logger;

import com.hd123.rumba.runtime.HibernateSession;
import com.hd123.rumba.runtime.NamingMgr;
import com.hd123.rumba.runtime.OperateContext;
import com.hd123.rumba.user.IUserMgr;
import com.hd123.rumba.util.StopWatch;

/**
 * @author caili
 */
public class DomainMgrTest {

  public static void main(String args[]) throws Exception {
    DomainMgrTest t = new DomainMgrTest();
    t.setUp();
    try {
//      t.testCreateDomain();
      t.testAddMiddleDomains();
    } catch (Exception e) {
      t.logger.warn(e.getMessage());
    }
    t.testGetDomain();
  }

  private IDomainMgr domainMgr;
  private Logger logger;

  protected void setUp() throws Exception {
    domainMgr = (IDomainMgr)NamingMgr.getInstance().lookupMgr(IDomainMgr.class);
    logger = Logger.getLogger(this.getClass());
  }

  public void testCreateDomain() throws Exception {
    Session sess = HibernateSession.getSession();
    IUserMgr userMgr = (IUserMgr) NamingMgr.getInstance().lookupMgr(IUserMgr.class);
    IDomain root = domainMgr.createDomain(null, "root", "Root Domain");
    IDomain tango = domainMgr.createDomain(root, "tango", "Tango Domain");
    IDomain tms = domainMgr.createDomain(root, "tms", "TMS domain");
    Transaction tx = sess.beginTransaction();
    domainMgr.saveDomain(root, new OperateContext(userMgr.getSysUser()), root.getOca());
    domainMgr.saveDomain(tango, new OperateContext(userMgr.getSysUser()), tango.getOca());
    domainMgr.saveDomain(tms, new OperateContext(userMgr.getSysUser()), tms.getOca());
    tx.commit();
    sess.flush();
    logger.info("Domain root, tango, and tms are created.");
  }

  public void testGetDomain() throws Exception {
    logger.info("domainMgr.getDomainById(\"root\")");
    IDomain root = domainMgr.getDomainByCode("root1");
    if (root == null) {
      logger.info("No domain with code=root1 found");
    } else {
      logger.info("Root: code=" + root.getCode() + " name=" + root.getName());
    }

    logger.info("domainMgr.createRootDomainsList()");
    List domains = domainMgr.createRootDomainsList();
    for (int i = 0; i < domains.size(); ++i) {
      root = (Domain) domains.get(i);
      logger.info("Root: code=" + root.getCode() + " name=" + root.getName());
      List children = domainMgr.createChildDomainsList(root);
      for (int j = 0; j < children.size(); ++j) {
        Domain child = (Domain) children.get(j);
        logger.info("Child: code=" + child.getCode() + " name=" + child.getName());
      }
    }
  }
  
  public void testAddMiddleDomains() throws Exception {
    ArrayList list = new ArrayList();
    IDomainMgr domainMgr = (IDomainMgr)NamingMgr.getInstance().lookupMgr(IDomainMgr.class);
    list.add(domainMgr.getDomainByCode("mystore"));
    list.add(domainMgr.getDomainByCode("0101"));
    list.add(domainMgr.getDomainByCode("0102"));
    list.add(domainMgr.getRootDomain());
    StopWatch sw = new StopWatch();
    sw.start();
    domainMgr.appendMiddleDomains(list);
    sw.stop();
    System.out.println(sw.getTime());
    for (int i = 0; i < list.size(); ++i) {
      System.out.println( i + ": " + ((Domain)list.get(i)).getCode());
    }
  }
}
